package com.jz.yanshe.dialog;

import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.jz.yanshe.base.BaseDialogFragment;
import com.jz.jd.rest.business.RestProxy;
import com.jz.jd.rest.response.AreaResponse;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import com.jz.yanshe.util.PreferencesHelper;
import com.jz.yanshe.R;

/**
 * Created by Administrator on 2017/11/23.
 */

public class AreaDialogFragment extends BaseDialogFragment {

    public static AreaDialogFragment newInstance() {
        return new AreaDialogFragment();
    }

    private Dialog mDialog;
    private OnItemClickListener mListener;
    private TextView tv_area;
    private EditText edt_age;
    String province,city,area;
    String provinceid,cityid,areaid;
    RecyclerView recyclerView_province, recyclerView_city, recyclerView_area;
    MyAdapter myAdapter_province,myAdapter_area,myAdapter_city;
    List<AreaResponse.Area> list_province = new ArrayList<>();
    List<AreaResponse.Area> list_city = new ArrayList<>();
    List<AreaResponse.Area> list_area = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_area, container);
//        btn_confirm = view.findViewById(R.id.btn_confirm);
        myAdapter_province = new MyAdapter(list_province);
        myAdapter_area = new MyAdapter(list_area);
        myAdapter_city = new MyAdapter(list_city);
        recyclerView_province = view.findViewById(R.id.recyclerView_province);
        recyclerView_province.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView_province.setAdapter(myAdapter_province);
        recyclerView_city = view.findViewById(R.id.recyclerView_city);
        recyclerView_city.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView_city.setAdapter(myAdapter_city);
        recyclerView_area = view.findViewById(R.id.recyclerView_area);
        recyclerView_area.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView_area.setAdapter(myAdapter_area);
        getProvince();


        tv_area = view.findViewById(R.id.tv_area);
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = super.onCreateDialog(savedInstanceState);
        mDialog.setCancelable(false);
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity(Gravity.BOTTOM);
//            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        return mDialog;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        super.onActivityCreated(savedInstanceState);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0x00000000));
        getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
    }

    public OnItemClickListener getmListener() {
        return mListener;
    }

    public void setmListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }


    public interface OnItemClickListener {

        void onConfirm(String ids,String address);
    }

    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }


    class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {

        List<AreaResponse.Area> list;

        public MyAdapter(List<AreaResponse.Area> list) {
            this.list = list;
        }


        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            Holder holder = new Holder(getLayoutInflater().inflate(R.layout.item_area, parent, false));
            return holder;
        }

        @Override
        public void onBindViewHolder(final Holder holder, final int position) {
            holder.tv_name.setText(list.get(position).address_name);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (list.get(position).order_seq == 1) {
                        getCity(list.get(position).address_id);
                        province = list.get(position).address_name;
                        provinceid = list.get(position).address_id;
                        tv_area.setText(province);
                    } else if (list.get(position).order_seq == 2) {
                        getArea(list.get(position).address_id);
                        city = list.get(position).address_name;
                        cityid = list.get(position).address_id;
                        tv_area.setText(province+ "   "+city);
                    }else if(list.get(position).order_seq == 3){
                        area = list.get(position).address_name;
                        areaid = list.get(position).address_id;
                        tv_area.setText(province+ "   "+city+ "   "+area);
                        mListener.onConfirm(provinceid+","+cityid+","+areaid,province+ "   "+city+ "   "+area);
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class Holder extends RecyclerView.ViewHolder {
            TextView tv_name;

            public Holder(View itemView) {
                super(itemView);
                tv_name = itemView.findViewById(R.id.tv_name);
            }
        }
    }


    private void getProvince() {
        RestProxy.getInstance().getProvince(PreferencesHelper.getInstance().getString(getActivity(),"token"),new Observer<AreaResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(AreaResponse areaResponse) {
                list_province.clear();
                list_province.addAll(areaResponse.list);
                System.out.println(list_province.size()+ "  aaaaaaaaaaaaa");
                myAdapter_province.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }


    private void getArea(String address_id) {
        RestProxy.getInstance().getArea(address_id, new Observer<AreaResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(AreaResponse areaResponse) {
                list_area.clear();
                list_area.addAll(areaResponse.list);
                myAdapter_area.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void getCity(String address_id) {
        RestProxy.getInstance().getCity(address_id, new Observer<AreaResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(AreaResponse areaResponse) {
                list_city.clear();
                list_city.addAll(areaResponse.list);
                myAdapter_city.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }


}
